package cn.jxau.fukin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.jxau.fukin.model.ApplyUser;
import cn.jxau.fukin.model.CardUser;
import cn.jxau.fukin.model.Notify;
import cn.jxau.fukin.model.TradeUser;
import cn.jxau.fukin.model.User;
import cn.jxau.fukin.service.IApplyService;
import cn.jxau.fukin.service.ICardService;
import cn.jxau.fukin.service.INotifyService;
import cn.jxau.fukin.service.ITradeService;
import cn.jxau.fukin.service.IUserService;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping(value = "/userInfo")
public class UserInfoController {
	@Autowired
	IApplyService applyService;

	@Autowired
	ICardService cardService;

	@Autowired
	ITradeService tradeService;

	@Autowired
	IUserService userService;
	@Autowired
	INotifyService notifyService;

	/**
	 * 个人资料
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/showUserInfoPage.do")
	public String showUserInfoPage(HttpServletRequest request) {
		String flag = (String) request.getSession().getAttribute("flag");
		if (flag != null) {
			User user = (User) request.getSession().getAttribute("user");
			List<User> userList = userService.queryUserInfo(user,1);
			request.getSession().setAttribute("user", userList.get(0));
			return "forward:/WEB-INF/jsp/userInfor.jsp";
		} else {
			return "forward:/user/showLoginPage.do";

		}
	}

	/**
	 * 交易记录
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/showChangeInfoPage.do")
	public String showChangeInfoPage(HttpServletRequest request) {
		String flag = (String) request.getSession().getAttribute("flag");
		if (flag != null) {
			User user = (User) request.getSession().getAttribute("user");
			List<TradeUser> tradeUserList = tradeService.selectTradeByUser(user);
			request.getSession().setAttribute("list", tradeUserList);
			return "forward:/WEB-INF/jsp/changeInfo.jsp";
		} else {
			return "forward:/user/showLoginPage.do";

		}
	}

	/**
	 * 通知处理
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/showNoticePage.do")
	public String showNoticePage(HttpServletRequest request,
			@RequestParam(required = true, defaultValue = "1") Integer page,
			@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
		String flag = (String) request.getSession().getAttribute("flag");
		if (flag != null) {
			User user = (User) request.getSession().getAttribute("user");
			PageInfo<Notify> notifyResult = notifyService.selectNotifyByUser(user, page, 8);
			List<Notify> notifyList01 = new ArrayList<Notify>();
			for (int i = 0; i < 8; i++) {
				notifyList01.add(notifyResult.getList().get(i));
			}
			request.getSession().setAttribute("list", notifyList01);
			return "forward:/WEB-INF/jsp/noticePage.jsp";
		} else {
			return "forward:/user/showLoginPage.do";

		}
	}

	/**
	 * 银行卡信息页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/showCardInfoPage.do")
	public String showCardInfoPage(HttpServletRequest request) {
		String flag = (String) request.getSession().getAttribute("flag");

		if (flag != null) {
			User user = (User) request.getSession().getAttribute("user");
			List<CardUser> cardInfo = cardService.queryCardInfo(user);
			request.getSession().setAttribute("list", cardInfo);
			return "forward:/WEB-INF/jsp/cardInfo.jsp";
		} else {
			return "forward:/user/showLoginPage.do";

		}
	}

	/**
	 * 申请信息反馈页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/showApplyInfoPage.do")
	public String showApplyInfoPage(HttpServletRequest request) {
		String flag = (String) request.getSession().getAttribute("flag");
		// 验证用户登录后，将该用户的申请数据从数据库获取返回到页面进行显示
		if (flag != null) {
			User user = (User) request.getSession().getAttribute("user");
			List<ApplyUser> list = applyService.queryApplyInfo(user);
			request.getSession().setAttribute("list", list);
			return "forward:/WEB-INF/jsp/applyInfo.jsp";
		} else {
			return "forward:/user/showLoginPage.do";

		}
	}

	@RequestMapping(value = "/updateUserInfo.do")
	public ModelAndView updateUserInfo(ModelAndView mv, User user, HttpServletRequest request) {
		userService.updataUserInfo(user);
		request.getSession().setAttribute("user", user);
		mv.setViewName("redirect:/userInfo/showUserInfoPage.do");
		return mv;
	}

	@RequestMapping(value = "/updateNotifyInfo.do")
	public ModelAndView updateNotifyInfo(ModelAndView mv, Notify notify, HttpServletRequest request) {
		System.out.println(notify);
		notifyService.updateNotifyInfo(notify);
		mv.setViewName("redirect:/userInfo/showNoticePage.do");
		return mv;
	}
}
